<?php

// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      

//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2023/03/22
//      |_|                                         | TpMeCMS

declare(strict_types=1);

namespace ImiApp\ApiServer\Api\Controller\user;

use Imi\Db\Db;
use Imi\Server\Http\Route\Annotation\Action;
use Imi\Server\Http\Route\Annotation\Controller;
use Imi\Server\Http\Route\Annotation\Route;
use Psr\Http\Message\ResponseInterface;
use Imi\Aop\Annotation\Inject;
// use Phpben\Imi\Validate\Annotation\Validate;
use Phpben\Imi\Auth\Annotation\Auth;


/**
 * 任务积分🕹️
 * @Auth(name="token",nologin={"no"})
 * @Controller("/user.taskscore/")
 */
class Taskscore extends UserBean
{
    protected $noNeedLogin = ['no'];
    protected $noNeedRight = ['*'];


    /**
     * 获取任务积分列表📸
     *
     * @param string $status 类型:2=每日任务,1=新生任务
     * @param integer $page 1
     * @param integer $limit 30
     * @return void
     * @Action
     */
    public function get_taskscore_list(?int $page = 1, ?int $limit = 30, $status = null)
    {
        $page = $page ?: 1;
        $limit = $limit ?: 30;
        $where = [];
        if ($status) {
            $where['status'] = $status; //类型:2=每日任务,1=新生任务
            // $date = date('Y-m', strtotime($date));
            // $e_date = date('Y-m-d 23:59:59', strtotime($date . " +1 month -1 day"));
            // $data['createtime'] = ['between time', [$date, $e_date]];
        }
        // $where['user_id'] = $this->auth->user()->id;
        $list = Db::name('taskscore_set')->whereEx($where)->orderRaw('weigh desc')->page($page, $limit)->select()->getArray();
        if ($list)
            foreach ($list as $key => &$val) {
                $this->ChangeFieldData($val);
            }
        return $this->success('ok', $list);
    }


    /**
     * 改变字段
     *
     * @param array $val 
     * @return void
     */
    protected function ChangeFieldData(&$val)
    {
        // 1	每日签到
        // 2	用户评价
        // 3	邀请注册
        // 4	看视频
        // 5	分享产品
        // 6	首次参与
        // 8	首次登陆
        // 9	完善身份信息
        // 10	提现账号绑定
        // 11	平台规则学习
        // 12	关注微信公众号
        switch ($val['id']) {
            case '1': //每日签到
                $val['task'] = $this->isTodaySign();
                break;

            case '2': //用户评价
                $val['task'] = $this->isTodaySign();
                break;

            case '3': //邀请注册
                $val['task'] = $this->isTodayInviteReg();
                break;

            case '4': //看视频
                $val['task'] = $this->isTodaySign();
                break;

            case '5': //分享产品
                $val['task'] = $this->isTodaySign();
                break;

            case '6': //首次参与
                $val['task'] = $this->isTodaySign();
                break;

            case '8': //首次登陆
                $val['task'] = $this->isFirstLogin();
                break;

            case '9': //完善身份信息
                $val['task'] = $this->isPerfectUserInfo();
                break;

            case '10': //提现账号绑定
                $val['task'] = $this->isTodaySign();
                break;

            case '11': //平台规则学习
                $val['task'] = $this->isTodaySign();
                break;

            case '12': //关注微信公众号
                $val['task'] = $this->isTodaySign();
                break;

            default:
                # code...
                break;
        }

        // $val['date_str'] = $this->TimeToStr($val['createtime']);
        return $val;
    }

    /**
     * 今日是否签到
     *
     * @return boolean
     */
    protected function isTodaySign()
    {

        $res = Db::name('signlog')->where('user_id', '=', $this->auth->user()->id)->where('date', '=', date('Y-m-d'))->count();
        if ($res) {
            return true;
        }
        return false;
    }

    /**
     * 是否第一次登陆
     *
     * @return boolean
     */
    protected function isFirstLogin()
    {
        $res = Db::name('user')->where('id', '=', $this->auth->user()->id)->where('`createtime`!=`logintime`')->count();
        if ($res) {
            return true;
        }
        return false;
    }

    /**
     * 是否完善用户信息
     *
     * @return boolean
     */
    protected function isPerfectUserInfo()
    {
        $user = Db::name('user')->where('id', '=', $this->auth->user()->id)->where('real_status', '=', '1')->cache(10)->select()->get();
        if ($user) {
            return true;
        }
        return false;
    }


    /**
     * 今日邀请注册
     *
     * @return boolean
     */
    protected function isTodayInviteReg()
    {
        $user = Db::name('user')->where('pid', '=', $this->auth->user()->id)->where('createtime', 'between', [strtotime(date('Y-m-d') . '00:00:00'), strtotime(date('Y-m-d') . '23:59:59')])->select()->get();
        if ($user) {
            return true;
        }
        return false;
    }
}
